package com.sunlee.admin.api.module.mall.controller;

import com.sunlee.admin.api.annotation.LogOperation;
import com.sunlee.admin.api.util.ExcelUtils;
import com.sunlee.common.core.constant.Constant;
import com.sunlee.common.core.entity.PageData;
import com.sunlee.common.core.entity.Result;
import com.sunlee.common.core.utils.AssertUtils;
import com.sunlee.common.core.validator.group.AddGroup;
import com.sunlee.common.core.validator.group.DefaultGroup;
import com.sunlee.common.core.validator.group.UpdateGroup;
import com.sunlee.common.service.dto.ProductDTO;
import com.sunlee.common.service.excel.ProductExcel;
import com.sunlee.common.service.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 商品表
 *
 * @author sunlee
 */
@RestController
@RequestMapping("/product")
@Api(tags = "商品表")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @RequiresPermissions("product:page")
    public Result<PageData<ProductDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        return Result.ok(productService.pageOwn(params));
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("product:info")
    public Result<ProductDTO> get(@PathVariable("id") Long id) {
        return Result.ok(productService.getOwn(id));
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("product:save")
    public Result<Object> save(@Validated({AddGroup.class, DefaultGroup.class}) @RequestBody ProductDTO dto) {
        dto.setCreateDate(new Date());
        dto.setUpdateDate(new Date());
        productService.saveOwn(dto);
        return Result.ok();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("product:update")
    public Result<Object> update(@Validated({UpdateGroup.class, DefaultGroup.class}) @RequestBody ProductDTO dto) {
        dto.setUpdateDate(new Date());
        productService.updateOwn(dto);
        return Result.ok();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("product:delete")
    public Result<Object> delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        productService.deleteRelByIds(ids);
        return Result.ok();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("product:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<ProductDTO> list = productService.listOwn(params);
        ExcelUtils.exportExcelToTarget(response, null, list, ProductExcel.class);
    }

}